<?php
// +----------------------------------------------------------------------
// | ThinkSNS
// +----------------------------------------------------------------------
// | Copyright (c) 2009 http://www.thinksns.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: Daniel Yang <desheng.young@gmail.com>
// +----------------------------------------------------------------------
//

/**
 +------------------------------------------------------------------------------
 * HrbiService 人力资源信息查询服务
 * 实现对HRBI WebService的封装
 +------------------------------------------------------------------------------
 * @category	addons
 * @package		addons
 * @subpackage  services
 * @author		Daniel Yang <desheng.young@gmail.com>
 * @version		$Id$
 +------------------------------------------------------------------------------
 */

class HrbiService extends Service {
	var $lang = '';			// chn: 中文 | eng: 英文
	var $output_type = '';	// json: JSON格式 | decoded_json: 对JSON进行json_decode后的结果

	public function __construct($data) {
		//服务初始化
		$this->init($data);
	}

	public function init($data = '') {
		if( empty($this->lang) ){
			$this->lang = 'chn';
		}
		if( empty($this->output_type) ) {
			$this->output_type = 'json';
		}
	}

	//运行服务
	public function run(){

	}

	//获取返回结果的语言
	public function getLang() {
		return $this->lang;
	}

	//设置返回结果的语言 [chn: 中文 | eng: 英文]
	public function setLang($lang) {
		$this->lang = $lang;
	}

	//获取返回结果的类型
	public function getOutputType() {
		return $this->output_type;
	}

	//设置返回结果的类型 [json: JSON格式 | decoded_json: 对JSON进行json_decode后的结果]
	public function setOutputType($output_type) {

		if ( ($output_type == 'json') || ($output_type == 'decoded_json') ) {
			$this->output_type = $output_type;
		}

	}

	//格式化返回结果
	protected function _format($result) {
		if($this->output_type == 'json'){
			return $result;
		}elseif ($this->output_type == 'decoded_json') {
			return json_decode($result);
		}
	}

	//根据url输出结果
	protected function _output($url) {
		if( ($result_json = file_get_contents($url)) != false ) {
			return $this->_format($result_json);
		}else {
			return false;
		}
	}

	//合成数组
	protected function _mergeArray($array1, $array2) {

	}

	/**
     +----------------------------------------------------------
     * 根据关键字查询人员ID列表
     +----------------------------------------------------------
     * @access public
     +----------------------------------------------------------
     * @param string $key   查询关键字, 可以是全拼、W3帐号、工号、CN名
     * @param string $scope 查询范围 [可选 | 默认值: 空]
     +----------------------------------------------------------
     * @return array | false
     +----------------------------------------------------------
     * @see $key   因性能考虑, 不支持输入部分拼音模糊查询;
     * 			   国外员工支持如下查询:
     * 			   Ferrand Teoh Phang Jin 00718426 可输入：Ferrand T 来查询, 
     * 		       即第一空格后加字母. [注意空格后一定要有字符才能模糊查询]
     * @see $scope "intranet": 内网 | "internet": 外网 | 为空时：内网查询
     * 
     +----------------------------------------------------------
     */
	public function getIDs($key, $scope = '') {
		$url = "http://w3.huawei.com/ws/IdServlet?key=" . urlencode($key) . "&lang={$this->lang}";
		if( $scope != '') {
			$url .= "&scope=$scope";
		}
		return $this->_output($url);
	}

	/**
     +----------------------------------------------------------
     * 根据工号查询人员信息
     +----------------------------------------------------------
     * @access public
     +----------------------------------------------------------
     * @param string $empNo 工号
     * @param ing    $full  标志值 [可选 | 默认值: 空]
     +----------------------------------------------------------
     * @return array
     +----------------------------------------------------------
     * @see $empNo 八位工号前面的零，可以不补齐
     * @see $full  非1: 返回常用信息 | "1": 返回全部信息
     +----------------------------------------------------------
     */
	public function getPersonDetail($empNo, $full = '') {
		$url = "http://w3.huawei.com/ws/PersonServlet?empNo=$empNo&lang={$this->lang}";
		if($full != '') {
			$url .= "&full=$full";
		}
		$result_json	= file_get_contents($url);
		$result_decoded = json_decode($result_json);
		if($result_decoded->errorInfo && !is_numeric($empNo[0])) {
			//取掉首字母重新查询
			$empNo = substr($empNo,1);
			$url = "http://w3.huawei.com/ws/PersonServlet?empNo=$empNo&lang={$this->lang}";
			if($full != '') {
				$url .= "&full=$full";
			}
			return $this->_output($url);
		}else {
			return $this->_format($result_json);
		}
	}

	/**
     +----------------------------------------------------------
     * 获取所给部门编号的下级部门列表
     +----------------------------------------------------------
     * @access public
     +----------------------------------------------------------
     * @param string $upperCode 上一级部门编号 [可选 | 默认值: 空]
     +----------------------------------------------------------
     * @return array
     +----------------------------------------------------------
     * @see $upperCode 为空时：返回所有的公司列表
     +----------------------------------------------------------
     */
	public function getDepartmentList($upperCode = '') {
		$url = "http://w3.huawei.com/ws/HRDeptListServlet?lang={$this->lang}";
		if($upperCode != '') {
			$url .= "&upperCode=$upperCode";
		}
		return $this->_output($url);
	}

	/**
     +----------------------------------------------------------
     * 获取所给部门编号的部门信息
     +----------------------------------------------------------
     * @access public
     +----------------------------------------------------------
     * @param string $code 部门编号
     +----------------------------------------------------------
     * @return array
     +----------------------------------------------------------
     */
	public function getDepartmentDetail($code) {
		$url = "http://w3.huawei.com/ws/HRDepartmentServlet?lang={$this->lang}&code=$code";
		return $this->_output($url);
	}

	/**
     +----------------------------------------------------------
     * 通过员工工号、部门编号获取权签人信息
     +----------------------------------------------------------
     * @access public
     +----------------------------------------------------------
     * @param string $code     员工工号或部门编号
     * @param string $category 权签人类型 [可选 | 默认值: 空]
     +----------------------------------------------------------
     * @return array
     +----------------------------------------------------------
     * @see $category 为空时，返回对应$code的所有权签人
     +----------------------------------------------------------
     */
	//注：目前为测试环境，仍为部署生产环境
	public function getApprover($code, $category = '') {
		$url = "http://oa-test.huawei.com/ws/HRApproverServlet?code=$code";
		if($category != '') {
			$url .= "&Category=$category";
		}
		return $this->_output($url);
	}

	/**
     +----------------------------------------------------------
     * 判断一个用户ID是否存在于给定的Notes群组
     +----------------------------------------------------------
     * @access public
     +----------------------------------------------------------
     * @param string $id    员工w3帐号、工号、或NotesCN名
     * @param string $group 群组名
     +----------------------------------------------------------
     * @return array
     +----------------------------------------------------------
     * @see $id 使用w3帐号性能最优
     +----------------------------------------------------------
     */
	public function isExistInNotesGroup($id, $group) {
		$url = "http://w3.huawei.com/ws/IsExistInNGServlet?id=" . urlencode($id) . "&group=" . urlencode($group);
		return $this->_output($url);
	}

	/**
     +----------------------------------------------------------
     * 用于群组名的查找，输入联想等
     +----------------------------------------------------------
     * @access public
     +----------------------------------------------------------
     * @param string $group 群组名
     * @param string $page  页码数 [可选 | 默认值: 空]
     +----------------------------------------------------------
     * @return array
     +----------------------------------------------------------
     * @see $group 群组名可以输入不完全
     * 			   e.g. "group=it-": 查询以"it-"开头的所有群组名
     * @see $page  页码数为空时，返回第1页 | 页码数大约总页数时，返回最后一页
     * 			   e.g. "page=1"时返回数组的"Array[more][0]=>50": 还有50条记录
     +----------------------------------------------------------
     */
	public function getNotesGroupListPage($group, $page = '') {
		$url = "http://w3.huawei.com/ws/NGListServlet?group=" . urlencode($group);
		if( $page != '' ){
			$url .= "&page=$page";
		}
		return $this->_output($url);
	}

	public function getAllNotesGroupList($group) {
		//TODO
	}

	/**
     +----------------------------------------------------------
     * 查询一个用户存在于哪些群组
     +----------------------------------------------------------
     * @access public
     +----------------------------------------------------------
     * @param string $id 员工w3帐号、工号、或NotesCN名等
     +----------------------------------------------------------
     * @return array
     +----------------------------------------------------------
     * @see $id 使用w3帐号性能最优
     +----------------------------------------------------------
     */
	public function getNotesGroupListByUserID($id) {
		$url = "http://w3.huawei.com/ws/getGroupByIdServlet?id=" . urlencode($id);
		return $this->_output($url);
	}

	/**
     +----------------------------------------------------------
     * 判断一个cn名和uid是否为一个有效的ID
     +----------------------------------------------------------
     * @access public
     +----------------------------------------------------------
     * @param string $cn  LDAP的cn名
     * @param string $uid LDAP的uid，即w3帐号
     +----------------------------------------------------------
     * @return array
     +----------------------------------------------------------
     * @see $cn  LDAP的cn名
     * @see $uid LDAP的uid，即w3帐号
     +----------------------------------------------------------
     */
	public function isValidId($cn, $uid) {
		$url = "http://w3.huawei.com/ws/IsIdServlet?cn=" . urlencode($cn) . "&uid=$uid";
		return $this->_output($url);
	}

	/* 后台管理相关方法 */

	//启动服务，未编码
	public function _start(){
		return true;
	}

	//停止服务，未编码
	public function _stop(){
		return true;
	}

	//卸载服务，未编码
	public function _install(){
		return true;
	}

	//卸载服务，未编码
	public function _uninstall(){
		return true;
	}
}
?>